package com.lzc.springboot_jsp_shiro.shirorealm;

import com.lzc.springboot_jsp_shiro.entity.Permission;
import com.lzc.springboot_jsp_shiro.entity.Role;
import com.lzc.springboot_jsp_shiro.service.PermissionService;
import com.lzc.springboot_jsp_shiro.service.RoleService;
import com.lzc.springboot_jsp_shiro.shirorealm.salt.MySalt;
import com.lzc.springboot_jsp_shiro.utils.ApplicationContextUtils;
import com.lzc.springboot_jsp_shiro.entity.User;
import com.lzc.springboot_jsp_shiro.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 自定义realm
 */
public class CustomerRealm extends AuthorizingRealm {

    /**
     * 授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取身份信息
        String principal = (String) principalCollection.getPrimaryPrincipal();
        //拿到userservice对象
        RoleService userService = (RoleService) ApplicationContextUtils.getBean("roleServiceImpl");
        //拿到permissionservice对象
        PermissionService permissionService = (PermissionService) ApplicationContextUtils.getBean("permissionServiceImpl");
        List<Role> roles = userService.findRoleByUsername(principal);
        if (!CollectionUtils.isEmpty(roles)) {
            //返回权限
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
            roles.forEach(role -> {
                authorizationInfo.addRole(role.getName());
                if (!ObjectUtils.isEmpty(role)) {
                    //得到对应权限
                    List<Permission> permissions = permissionService.findPermissionsByRid(role.getId());
                    if (!CollectionUtils.isEmpty(permissions)) {
                        permissions.forEach(permission -> {
                            //设置进去
                            authorizationInfo.addStringPermission(permission.getPerms());
                        });
                    }
                }
            });
            return authorizationInfo;
        }
        return null;
    }

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取用户身份信息 用户名
        String principal = (String) authenticationToken.getPrincipal();
        //拿到userservice对象
        UserService userService = (UserService) ApplicationContextUtils.getBean("userServiceImpl");
        User user = userService.login(principal);
        if (!ObjectUtils.isEmpty(user)) {
            //认证
            return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), new MySalt(user.getSalt()), this.getName());
        }
        return null;
    }
}
